﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using Newtonsoft.Json;
using SH.STD.Model;
using Cmn;
using SH.STD.DataProvider;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for Award
    /// </summary>
    public class Award : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "application/json;charset=gb2312";

            switch (context.Request.Params["action"])
            {
                case "loadData":
                    context.Response.Write(ConvertToJsonString(context.Request.Params["id"]));
                    break;
                case "addData":
                    context.Response.Write(AddData(context.Request.Params["jsonDataString"]));
                    break;
                case "removeData":
                    context.Response.Write(RemoveData(context.Request.Params["id"]));
                    break;
                case "updateData":
                    context.Response.Write(UpdateData(context.Request.Params["jsonDataString"]));
                    break;
            }
        }

        private string AddData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
INSERT INTO Award ([No] ,[Name] ,[Type] ,[ReceiveDate],[DepID] ,[Introduce],[Memo],[LastModifiedBy])
            VALUES (@No ,@Name ,@Type ,@ReceiveDate ,@DepID ,@Introduce ,@Memo ,@LastModifiedBy);SELECT @@identity";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("No",RepsonseInfo.Decode(item["No"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(item["Name"].ToString())),
                        new SqlParameter("Type",RepsonseInfo.Decode(item["Type"].ToString())),
                        new SqlParameter("ReceiveDate",item["ReceiveDate"]),
                        new SqlParameter("DepID",RepsonseInfo.Decode(item["DepID"].ToString())),
                        new SqlParameter("Introduce",RepsonseInfo.Decode(item["Introduce"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(item["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID });
        }
        private string ConvertToJsonString(string id)
        {
            var sql = @"
SELECT * 
FROM [Award] 
WHERE ID=@ID";
            var cmdParams = new SqlParameter[]
                {
                    new SqlParameter("ID",id.ToInt()),
                };
            var dt = Cmn.DB.getDataTable(sql, cmdParams);
            return DataAccess.ConvertToJSON(dt, "Awards");
        }
        private string UpdateData(string jsonDataString)
        {
            JavaScriptObject patent = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
Update Award Set 
No=@No,
Name=@Name,
Type=@Type,
ReceiveDate=@ReceiveDate,
DepID=@DepID,
Introduce=@Introduce,
Memo=@Memo,
LastModifiedBy=@LastModifiedBy
WHERE ID=@ID";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(patent["ID"].ToString())),
                        new SqlParameter("No",RepsonseInfo.Decode(patent["No"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(patent["Name"].ToString())),
                        new SqlParameter("Type",RepsonseInfo.Decode(patent["Type"].ToString())),
                        new SqlParameter("ReceiveDate",RepsonseInfo.Decode(patent["ReceiveDate"].ToString())),
                        new SqlParameter("DepID",RepsonseInfo.Decode(patent["DepID"].ToString())),
                        new SqlParameter("Introduce",RepsonseInfo.Decode(patent["Introduce"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(patent["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = patent["ID"].ToString() });
        }

        private string RemoveData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(item["ID"].ToString())),
                    };
            var sql = "update Award set status=0 where ID=@ID";
            Cmn.DB.execSql(sql, cmdParas);

            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = item["ID"].ToString() });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}